// Package controllers
// @Title  role.go
// @Description  角色控制器
// @Author  小斌 (时间 格式是2021/12/02  11:40)
// @Update  小斌 (时间 格式是2021/12/02  11:40)

package controllers

import (
	"beego-admin/library"
	"beego-admin/models"
	"beego-admin/service"
	"encoding/json"
	"strconv"
	"time"
)

type RoleController struct {
	BaseController
}

// Index 角色列表页
func (this *RoleController) Index() {
	this.TplName = "admin/system_setting/role.tpl"
}

// Add 角色添加页
func (this *RoleController) Add() {
	var menuAuthMap = make(map[int]int)
	menuList,_,_ := models.GetMenuList(9999,1,"")
	treeList := service.GetAuthMenuRecursive(menuList,0,menuAuthMap)
	treeListJson, _ := json.Marshal(treeList)

	this.Data["tree_list_json"] = string(treeListJson)
	this.TplName = "admin/system_setting/role_add.tpl"
}

// Edit 角色编辑页
func (this *RoleController) Edit() {
	id,_ := this.GetInt("id",0)

	roleInfo,_ := models.GetRoleInfoById(id)

	var menuAuthArr = make([]string,0)
	json.Unmarshal([]byte(roleInfo.Auth), &menuAuthArr)
	var menuAuthMap = make(map[int]int)
	for _,val := range menuAuthArr{
		menuId, _ := strconv.Atoi(val)
		menuAuthMap[menuId] = 1
	}

	menuList,_,_ := models.GetMenuList(9999,1,"")
	treeList := service.GetAuthMenuRecursive(menuList,0,menuAuthMap)
	treeListJson, _ := json.Marshal(treeList)

	this.Data["role_info"] = roleInfo
	this.Data["tree_list_json"] = string(treeListJson)

	this.TplName = "admin/system_setting/role_edit.tpl"
}

// AjaxGetRoleList  获取角色列表
func (this *RoleController) AjaxGetRoleList() {
	page,_ := this.GetInt("page",1)
	pageSize,_ := this.GetInt("limit",5)
	list, count, _ := models.GetRoleList(pageSize,page)
	type RoleObj struct{
		Id                 int    `json:"id"`
		RoleName               string `json:"role_name"`
		MenuName				string `json:"menu_name"`
		Desc                string `json:"desc"`
		IsDelete                int `json:"is_delete"`
		UpdateTime                time.Time `json:"update_time"`
		AddTime                time.Time `json:"add_time"`
	}
	var returnData[]RoleObj
	for _, v := range *list {
		var authMenuName = ""
		if v.Id == 1{
			authMenuName = "全部"
		}else{
			var menuAuthArr = make([]string,0)
			json.Unmarshal([]byte(v.Auth), &menuAuthArr)
			menuList,_ := models.GetMenuListByIds(library.String2Int(menuAuthArr),"")
			for _,menuVal := range *menuList{
				authMenuName += menuVal.Title+","
			}
		}
		tmpParam := RoleObj{v.Id,v.RoleName,authMenuName, v.Desc,v.IsDelete,v.UpdateTime,v.AddTime}
		returnData = append(returnData, tmpParam)
	}


	this.Data["json"] = this.AjaxLayuiTableReturn(0,"成功",count,returnData)
	this.ServeJSON()
}

// AjaxAdd ajax添加角色
func (this *RoleController) AjaxAdd() {
	roleName := this.GetString("role_name")
	desc := this.GetString("desc")
	menuAuthJson := this.GetString("menu_auth_json")

	id := models.AddRole(roleName,desc,menuAuthJson)
	if id > 0{
		this.Data["json"] = this.AjaxReturn(10000,"添加成功",nil)
	}else{
		this.Data["json"] = this.AjaxReturn(10001,"添加失败",nil)
	}

	this.ServeJSON()
}

// AjaxEdit ajax编辑角色
func (this *RoleController) AjaxEdit() {
	roleId,_ := this.GetInt("id",0)
	roleName := this.GetString("role_name")
	desc := this.GetString("desc")
	menuAuthJson := this.GetString("menu_auth_json")

	if roleId == 1{
		this.Data["json"] = this.AjaxReturn(10001,"超级管理员的角色不可修改",nil)
		this.ServeJSON()
		this.StopRun()
	}
	roleInfo,_ := models.GetRoleInfoById(roleId)
	if roleInfo.Id <= 0{
		this.Data["json"] = this.AjaxReturn(10001,"更新的记录不存在",nil)
		this.ServeJSON()
		this.StopRun()
	}

	id := models.UpdateRoleById(roleId,roleName,desc,menuAuthJson)
	if id > 0{
		this.Data["json"] = this.AjaxReturn(10000,"更新成功",nil)
	}else{
		this.Data["json"] = this.AjaxReturn(10001,"更新失败",nil)
	}

	this.ServeJSON()
}

// AjaxDelete ajax删除角色
func (this *RoleController) AjaxDelete() {
	roleId,_ := this.GetInt("id",0)

	if roleId == 1{
		this.Data["json"] = this.AjaxReturn(10001,"超级管理员角色不可删除",nil)
		this.ServeJSON()
		this.StopRun()
	}
	roleInfo,_ := models.GetRoleInfoById(roleId)
	if roleInfo.Id <= 0{
		this.Data["json"] = this.AjaxReturn(10001,"删除的记录不存在",nil)
		this.ServeJSON()
		this.StopRun()
	}

	num,_ := models.GetAdminUserCountByRoleId(roleId)
	if num > 0{
		this.Data["json"] = this.AjaxReturn(10001,"角色使用中,不可删除",nil)
		this.ServeJSON()
		this.StopRun()
	}
	id := models.DeleteRoleById(roleId)
	if id > 0{
		this.Data["json"] = this.AjaxReturn(10000,"删除成功",nil)
	}else{
		this.Data["json"] = this.AjaxReturn(10001,"删除失败",nil)
	}

	this.ServeJSON()
}